﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HR.Appointments.Service;

public partial class NewEntryAction : EmployeePage
{
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCredentials();
        if (!IsPostBack)
        {
            bool change = Convert.ToBoolean(Request.QueryString["Change"]);
            if (change)
            {
                var activity = Facade.Employee().GetActivityInstance(
                    GetCompanyIdFromCookie(),
                    Convert.ToInt32(Request.QueryString["ActivityId"]));
                _txtActivity.Text = activity.Name;
                _ddlDuration.Text = activity.Duration.ToString();
            }
        }
    }

    protected void _btnNewActivity_Click(object sender, EventArgs e)
    {
        int companyId = GetCompanyIdFromCookie();

        var activity = ActivityEntity.Create(
            companyId,
            _txtActivity.Text,
            Convert.ToInt32(_ddlDuration.Text));

        if(Convert.ToBoolean(Request.QueryString["Change"]))
        {
            activity.ActivityId = Convert.ToInt32(Request.QueryString["ActivityId"]);
            try
            {
                Facade.Employee().UpdateActivity(activity);
            }
            catch (SubmitException)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að uppfæra.');", true);
                return;
            }
        }
        else
        {
            try
            {
                Facade.Employee().CreateActivity(activity);
            }
            catch (SubmitException)
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "ErrorAlert", "alert('Ekki tókst að stofna.');", true);
                return;
            }
        }
		Server.Transfer("~/Config.aspx", true);
    }
    #endregion

    #region Link Transfer
    protected void _ibmHomePage_Click(object sender, ImageClickEventArgs e)
	{
        if (!IsPostBack || (e.X != 0 && e.Y != 0))
        {
            Server.Transfer("~/Company.aspx", true);
        }
	}

	protected void _lbtnConfig_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/Config.aspx",true);
	}

	protected void _lbtnCompanyBooking_Click(object sender, EventArgs e)
	{
		Server.Transfer("~/CompanyBooking.aspx", true);
    }
    #endregion
}
